// import { login } from '@/api/user'
import { login } from '@/api/user'
import { getToken, setToken } from '@/utils/auth'

const state = {
  token: getToken(),
  userinfo: JSON.parse(localStorage.getItem('setUserinfo'))
}
const mutations = {
  setToken(state, newToken) {
    state.token = newToken
    setToken(newToken)
  },
  setUserinfo(state, newUserInfo) {
    state.userinfo = newUserInfo
    setToken(newUserInfo)
  }
}
const actions = {
  async login(context, payload) {
    const token = await login(payload)
    localStorage.setItem('setUserinfo', JSON.stringify(token.data.user))
    context.commit('setUserinfo', token.data.user)
    context.commit('setToken', token.data.token.token)
  },
  logout(context) {
    // 做退出的操作
    // 清空 token, 个人信息
    context.commit('setToken', '')
    context.commit('setUserinfo', {})
  }
}
export default {
  namespaced: true, // 开启命名空间
  state,
  mutations,
  actions
}
